package cc.minos.question.plugins
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.utils.ByteArray;
	import cc.minos.question.Question;
	
	/**
	 * ...
	 * 题目插件基类
	 * @author Minos
	 */
	public class QuestionPlugin
	{
		//API
		public static const VERSION:Number = 2.0;
		public static const API:Number = 2.0;
		//插件类型
		public var propName:String;
		//public var acronym:String;
		//素材
		protected var skin:DisplayObjectContainer;
		//数据
		protected var data:Object;
		//分数
		protected var tScore:Number;
		//得分
		protected var _score:Number;
		//检查全部
		protected var checkAll:Boolean;
		//答案
		protected var answer:Array;
		//选项标识符
		protected var optionName:String = "a_$_obj";
		protected var options:Number = 0;
		
		public function QuestionPlugin()
		{
			//构造
		}
		
		/**
		 * 初始化插件
		 */
		protected function init():void
		{
			tScore = Number( getData( "score" ) );
			var s:String = getData("all")?String( getData("all") ):String( getData("checkAll") );
			checkAll = s.toUpperCase() == "TRUE"?true:false;
			answer = getAnswer();
			_score = 0;
		}
		
		/**
		 * 获取参数
		 * @param	v	:	参数
		 * @return
		 */
		protected final function getData( v:String ):Object
		{
			if ( data[ v ] )
				return data[ v ];
			return null;
		}
		
		/**
		 * 检查是否有该参数
		 * @param	v	:	参数
		 * @return
		 */
		protected final function hasData( v:String ):Boolean
		{
			return data[ v ] ? true : false;
		}
		
		/**
		 * 获取素材中的可视对象
		 * @param	name
		 * @return
		 */
		protected final function getChild( name:String ):DisplayObject
		{
			var d:DisplayObject = null;
			if ( skin && skin is DisplayObjectContainer )
				d = skin.getChildByName( name );
			return d;
		}
		
		/**
		 * 获取答案数组（深复制）
		 * @return
		 */
		protected final function getAnswer():Array
		{
			var ba:ByteArray = new ByteArray();
			ba.writeObject( getData( "answer" ) );
			ba.position = 0;
			return ba.readObject() as Array;
		}
		
		/**
		 * 绑定素材和数据
		 * @param	skin
		 * @param	data
		 */
		public function bind( skin:DisplayObjectContainer , data:Object = null ):void
		{
			this.data = data;
			this.skin = skin;
			init();
		}
		
		/**
		 * 启动
		 */
		public function activate():void
		{
			addListeners();
		}
		
		/**
		 * 取消
		 */
		public function cancel():void
		{
			removeListeners();
		}
		
		/**
		 * 重置
		 */
		public function reset():void
		{
			//to protected
		}
		
		/**
		 * 添加侦听
		 */
		protected function addListeners():void
		{
			//to protected
		}
		
		/**
		 * 移除侦听
		 */
		protected function removeListeners():void
		{
			//to protected
		}
		
		/**
		 * 检查
		 * @return
		 */
		public function check():Boolean
		{
			return false;
		}
		
		/**
		 * 获取得分
		 */
		public function get score():Number
		{
			return _score;
		}
		
		/**
		 * 启动插件
		 * @param	plugins
		 * @return
		 */
		public static function activate( plugins:Array ):Boolean
		{
			var i:int = plugins.length;
			while ( --i > -1 )
			{
				if ( plugins[ i ].API == QuestionPlugin.API )
				{
					Question.activatedPlugins[( new ( plugins[ i ] as Class )() ).propName ] = plugins[ i ];
					//Question.activatedPlugins[( new ( plugins[ i ] as Class )() ).acronym ] = plugins[ i ];
				}
			}
			return true
		}
	
	}

}